package cn.itcast.mobile_weather;

import android.util.Log;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpUtil {

    /**
     * 从指定的URL中获取数据
     *
     * @param urlPath
     * @return
     * @throws Exception
     */
    public static String readParseokhttp(String urlPath) throws Exception {

        OkHttpClient okHttpClient = new OkHttpClient();
        //请求
        final Request request = new Request.Builder()
                .url(urlPath)
                .build();
        //调用
        final Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();

    }

    /*
  带有错误处理接口的网络访问
  callback:回调
   */
    public static void sendOkHttpRequest(String urlPath, okhttp3.Callback callback) {
        Log.e("sendOkHttpRequest",urlPath);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(urlPath).build();
        client.newCall(request).enqueue(callback);
    }


}
